package com.rr.jvm.demo;

/**
 * @author RR
 * @date 2022/9/7 21:01
 */

/*
* 内存诊断
* 1. jps：查看当前系统中有哪些java进程
* 2. jmap工具：查看堆内存占用情况(抓取某一时刻的内存使用情况)
*
* 3. jconsole工具：直接启动java程序后在控制台输入jconsole，选择进程连接，选不安全连接
* 4. jvisualvm：jvisualvm也是jdk自带的，比jconsole再高级一点，可以dump堆下来
* */
public class Demo1ShowJvmMessage {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("1...启动程序，啥事没做");
        // 1. 输入指令jps，查找到Demo1ShowJvmMessage的进程号2828
        // 2. 执行jmap -heap 2828
        Thread.sleep(20000);
        byte[] bytes = new byte[1024 * 1024 * 10]; // 10MB
        System.out.println("2...分配10MB内存给对象");
        // 3. jmap -heap 2828
        Thread.sleep(20000);
        bytes = null;
        System.gc();
        System.out.println("3...手动触发一次GC");
        // 4. jmap -heap 2828
        Thread.sleep(20000);
    }
}
// 三次jmap信息如下
/*
Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 2118123520 (2020.0MB)
   NewSize                  = 44564480 (42.5MB)
   MaxNewSize               = 705691648 (673.0MB)
   OldSize                  = 89653248 (85.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 34078720 (32.5MB)
   used     = 4771216 (4.5501861572265625MB)
   free     = 29307504 (27.949813842773438MB)
   14.000572791466347% used
From Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
To Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
PS Old Generation
   capacity = 89653248 (85.5MB)
   used     = 0 (0.0MB)
   free     = 89653248 (85.5MB)
   0.0% used

3169 interned Strings occupying 259952 bytes.
====================================================
Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 2118123520 (2020.0MB)
   NewSize                  = 44564480 (42.5MB)
   MaxNewSize               = 705691648 (673.0MB)
   OldSize                  = 89653248 (85.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 34078720 (32.5MB)
   used     = 15256992 (14.550201416015625MB)
   free     = 18821728 (17.949798583984375MB)
   44.76985051081731% used
From Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
To Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
PS Old Generation
   capacity = 89653248 (85.5MB)
   used     = 0 (0.0MB)
   free     = 89653248 (85.5MB)
   0.0% used

3170 interned Strings occupying 260024 bytes.
================================================
Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 2118123520 (2020.0MB)
   NewSize                  = 44564480 (42.5MB)
   MaxNewSize               = 705691648 (673.0MB)
   OldSize                  = 89653248 (85.5MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 34078720 (32.5MB)
   used     = 681592 (0.6500167846679688MB)
   free     = 33397128 (31.84998321533203MB)
   2.0000516451322117% used
From Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
To Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
PS Old Generation
   capacity = 89653248 (85.5MB)
   used     = 1004704 (0.958160400390625MB)
   free     = 88648544 (84.54183959960938MB)
   1.1206554390533625% used

3156 interned Strings occupying 259048 bytes.

 */
